<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<ul class="breadcrumb panel">
    <li><a href="javascript:backMainFun();"><i class="fa fa-home"></i> 首页</a></li>
    <li><a href="javascript:loadFun('/org/toOrgIndex');">组织机构</a></li>
    <li class="active">新增组织</li>
</ul>

<div class="panel">
    <header class="panel-heading">
        新增组织
    </header>
    <div class="panel-body">
        <form action="${ctx}/user/doSave" name="userForm" id="userForm" method="post" class="form-horizontal">
            <div class="form-group has-feedback">
                <label for="parent_name" class="col-sm-2 control-label must">上级组织</label>
                <div class="col-sm-8">
                    <input type="hidden" id="parent_id" value="">
                    <input type="text" class="form-control" id="parent_name" name="parent_name" placeholder="请选择所属部门">
                    <i class="fa fa-sitemap form-control-feedback"></i>
                </div>
            </div>
            <div class="form-group">
                <label for="name" class="col-sm-2 control-label must">组织简称</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="name" name="name" placeholder="请输入组织简称">
                </div>
            </div>
            <div class="form-group">
                <label for="full_name" class="col-sm-2 control-label must">组织全称</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="full_name" name="full_name" placeholder="请输入组织全称">
                </div>
            </div>
            <div class="form-group">
                <label for="org_code" class="col-sm-2 control-label must">组织代码</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="org_code" name="org_code" placeholder="请输入组织代码">
                </div>
            </div>
            <div class="form-group">
                <label for="sort_no" class="col-sm-2 control-label must">顺序</label>
                <div class="col-sm-8">
                    <input type="number" class="form-control" id="sort_no" name="sort_no" placeholder="请输入组织顺序" >
                </div>
            </div>
            <div class="row">
                <div class="col-xs-10">
                    <button type="button" class="btn btn-primary pull-right" id="subBtn"><i
                            class="glyphicon glyphicon-saved"></i> 保存
                    </button>
                    <button type="button" class="btn btn-default pull-right marR" onclick="backFun()"><i
                            class="glyphicon glyphicon-chevron-left"></i> 返回
                    </button>
                </div>
            </div>

        </form>
    </div>
</div>

<div class="modal fade" id="orgModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">选择组织机构</h4>
            </div>
            <div class="modal-body">
                <div class="container-fluid" style="background: #f8f8f8;border-radius: 3px;max-height: 500px;overflow: auto">
                    <div id="treeApp" class="ztree"></div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><i class="glyphicon glyphicon-off"></i> 关闭</button>
                <button id="checkorgOk" type="button" class="btn btn-primary"><i class="glyphicon glyphicon-ok"></i> 确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    function backFun() {
        loadFun("/org/toOrgIndex");
    }
    $('#full_name').blur(function () {
        if ($('#full_name').val() != false) {
            $.post("${ctx}/org/checkName",
                    {
                        name: $('#full_name').val(),
                        parent_id: $('#parent_id').val(),
                    }
                    , function (data) {
                        if (!data.valid) {
                            zAlert("上级组织下已存在该名称，请重新填写!");
                            $('#full_name').focus();
                            $('#subBtn').attr("disabled",true);
                        }else{
                            $('#subBtn').attr("disabled",false);
                        }
                    });
        }
    });
    $('#name').focus(function () {
        if($('#parent_id').val()== false){
            zAlert("请先选择上级组织!");
            $('#name').blur();
        }
    });
    $('#org_code').blur(function () {
        if ($('#org_code').val() != false) {
            $.post("${ctx}/org/checkCode",
                    {
                        org_code: $('#org_code').val()
                    }
                    , function (data) {
                        if (!data.valid) {
                            zAlert("组织代码已存在，请重新填写!");
                            $('#org_code').focus();
                            $('#subBtn').attr("disabled",true);
                        }else{
                            $('#subBtn').attr("disabled",false);
                        }
                    });
        }
    });

    $('#subBtn').click(function () {
        if ($('#parent_id').val() == false) {
            zAlert('上级组织不能为空')
            return false;
        }else if ($('#name').val() == false) {
            zAlert('组织简称不能为空')
            return false;
        } else if ($('#full_name').val() == false) {
            zAlert('组织全称不能为空')
            return false;
        }else if ($('#org_code').val() == false) {
            zAlert('组织代码不能为空')
            return false;
        } else if ($('#sort_no').val() == false) {
            zAlert('组织顺序不能为空')
            return false;
        } else {
            $.ajax({
                url: "${ctx}/org/save",
                type: 'post',
                data: {
                    name: $('#name').val(),
                    full_name: $('#full_name').val(),
                    org_code: $('#org_code').val(),
                    parent_id: $('#parent_id').val(),
                    sort_no:$('#sort_no').val()
                },
                success: function (data) {
                    if (data.success) {
                        zAlert(data.msg);
                        setTimeout(function () {
                            loadFun("/org/toOrgIndex");
                        }, 1000);
                    } else {
                        zAlert(data.msg);
                    }
                }
            })
        }
    });

    //   部门组织树及选择部门代码
    var zTreeObj;
    var zNodes;
    function  filter(treeId, parentNode, data) {
        if (!data) return null;
        var childNodes = [];
        childNodes = data;

        for(var i=0;i<childNodes.length;i++){
            childNodes[i].isParent = true;
        };

        return childNodes;
    };

    $.post('${ctx}/org/getOrgTree',{
        parentId:'',
    },function (data) {
        var setting = {
            view: {
                showLine: false,
                expandSpeed:'fast'
            },
            async: {
                enable: true,
                contentType: "application/x-www-form-urlencoded",
                url: "${ctx}/org/getOrgTree",
                dataType: "text",
                type:'post',
                autoParam: ["org_id=parentId"],
                dataFilter: filter
            },
            check:{
                enable: true,
                chkStyle: "radio",
                radioType: "all"
            }
        };

        zNodes = data;
        zNodes[0].open=true;
        zNodes[0].isParent = true;

        for(var i=0;i<zNodes[0].children.length;i++){
            zNodes[0].children[i].isParent = true;
        };

        zTreeObj = $.fn.zTree.init($("#treeApp"), setting, zNodes);
    });
    //部门组织树及选择部门代码 end

    $('#parent_name').focus(function(){
        $('#orgModal').modal('show');
        $(this).blur();
    });
    $('#checkorgOk').click(function(){
        var nodes=zTreeObj.getCheckedNodes(true);
        $('#parent_name').val(nodes[0].full_name);
        $('#parent_id').val(nodes[0].org_id);
        $('#orgModal').modal('hide');
    });

</script>

